Send generic events with an API client
Collect user interactions as events on your website.
Capturing real-time user interactions as events gives you actionable insights with click and conversion metrics, and they help you increase your customer engagement and conversions. Events are used to activate Algolia features and products like:
- NeuralSearch
- Algolia Recommend
- Personalization
- Dynamic Re-Ranking
- Query Categorization
- Search analytics
- Revenue analytics
- A/B testing.
Enable events collection
Set the clickAnalytics
parameter to true
when making search requests.
This includes the queryID
parameter in the search response,
which is required for linking events and search requests.
Initialize search-insights.js
Install the search-insights
library.
After installing the search-insights
library, initialize the client in your website.
Understand which events to track
To understand which events you should send, think about the different steps a user takes through your website to reach the final goal: the product purchase.
Keep track of query IDs
Conversion events are often triggered outside of the search results page. In order to correctly attribute which query is responsible for a conversion, it is necessary to keep track of the query ID returned when a search is performed so that it can be included in conversion events.
To relate the conversion events back to the search request made on your search results or category pages, you need to keep track of the query IDs across your pages.
Track click events
Click events capture when users indicate they want to view specific pieces of content. Add the following code to track a user navigating to a piece of content from a search results page:
In clickedObjectIdsAfterSearch
:
queryID
is used by Algolia to relate the event to a prior search or browse event.objectIDs
should contain the ID of the article. This can be retrieved from theobjectID
field of thehits
array returned from the search request.positions
indicates the position of the item in the search results. For example, if the user clicked on the top result on the page, positions should be[1]
.
If a user found a piece of content without performing a search---for example, they browsed there from elsewhere on your site, add the following code to track that:
Track conversion events
An example of a conversion event that may be relevant to your media site:
The window.aa
object is the API client for the Insights API and is globally available
if you enabled automatic events collection.
In convertedObjectIDsAfterSearch
:
queryID
is used by Algolia to relate the event to a prior search.objectIDs
indicates which items were part of the conversion.TheobjectID
parameter is included in the search response for each hit.
If the event isn’t directly related to a search, for example, it’s triggered on the homepage,
use the convertedObjectIDs
method instead.
Some other examples of conversion events are:
Article Bookmarked
: a user bookmarked an article.Article Liked
: a user liked an article.Article Recommended
: a user recommended an article.Playlist Followed
: a user followed a playlist.Author Followed
: a user followed an author.Brand Followed
: a user followed a brand.
For Algolia Recommend and Personalization, capture additional click events when users select individual items:
- On your home page
- From recommendations
To send click events with the Insights client, add the following code whenever a user clicks on an article---for example, on your home page.
(Optional) Handle known users for Personalization
For effective personalization, you need to identify users across sessions. It’s best to use an identifier from your authentication system after users signed in.
After getting the identifier from your system,
set it as authenticatedUserToken
parameter.
When you set the authenticatedUserToken
parameter in the Insights client, you also need to update
the user token you send with your search requests.
If you can’t get persistent user identifiers from your system, you can store the anonymous user token in a cookie after obtaining user consent.
For more information, see User token.
(Optional) Send view events for Personalization
Personalization benefits from the same click and conversion events, plus it can use view events to enrich user profiles.
To capture view events, such as when a user views search results, add this code: